import { Component, Vue, Inject } from 'vue-property-decorator';

import { IRoomCommunity } from '@/shared/model/room-community.model';
import RoomCommunityService from './room-community.service';
import AlertService from '@/shared/alert/alert.service';

@Component
export default class RoomCommunityDetails extends Vue {
  @Inject('roomCommunityService') private roomCommunityService: () => RoomCommunityService;
  @Inject('alertService') private alertService: () => AlertService;

  public roomCommunity: IRoomCommunity = {};

  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (to.params.roomCommunityId) {
        vm.retrieveRoomCommunity(to.params.roomCommunityId);
      }
    });
  }

  public retrieveRoomCommunity(roomCommunityId) {
    this.roomCommunityService()
      .find(roomCommunityId)
      .then(res => {
        this.roomCommunity = res;
      })
      .catch(error => {
        this.alertService().showHttpError(this, error.response);
      });
  }

  public previousState() {
    this.$router.go(-1);
  }
}
